define(['jquery', 'nicescroll', 'pajax', 'viewport'], function($, nicescroll, pajax, viewport) {
  var initModule, scroll, router, navactive, navhover, pageajax, animated_contents, backtotop;
  jQuery.exists = function(selector) {
    "use strict";
    return (jQuery(selector).length > 0);
  };
  router = function() {
    var action = $('article').attr('data-main');
    if (action) {
      require(['../lib/' + action], function(action) {
        action.initModule(function(a) {
          navactive(a);
        });
      });
    }
  };

  animated_contents = function() {
    "use strict";
    if ($.exists('.animate-element')) {
      $(".animate-element:in-viewport").each(function(i) {
        var $this = $(this);
        if (!$this.hasClass('in-viewport')) {
          setTimeout(function() {
            $this.addClass('in-viewport');
          }, 100 * i);
        }
      });
    }
  }
  navactive = function(i) {
    $('header nav a').removeClass('on');
    $('header nav a').eq(i).addClass('on');
  };

  scroll = function() {

    $("html").niceScroll({
      cursorcolor: '#0173b9',//#CC0071 光标颜色
    });
  };
  pageajax = function() {
    if ($.support.pjax) {
      // $(document).pjax('a[data-pjax]', 'main', { // 本页面的ID
      $(document).pjax('a', 'main', { // 本页面的ID
        fragment: 'main',
        timeout: 5000,
        show: 'fade',
        cache: true,
        push: true,
        // replace: false,
        scrollTo: 0,
      });

      $(document).on('pjax:send', function() { //pjax链接点击后显示加载动画

        $("#progress").removeClass("done"); //完成，隐藏进度条
        $({
          property: 0
        }).animate({
          property: 100
        }, {
          duration: 1000,
          step: function() {
            var percentage = Math.round(this.property);

            $('#progress').css('width', percentage + "%");

            if (percentage == 100) {
              $("#progress").addClass("done"); //完成，隐藏进度条
            }
          }
        });
      });

      //加载完成后替换title
      $(document).on('pjax:success', function(data, status, xhr, options) {});
      $(document).on('pjax:complete', function() {
        $("#progress").addClass("done"); //完成，隐藏进度条

      });
      $(document).on('pjax:end', function(data, status, xhr, options) {
        router();
        //console.log(data.currentTarget.title);
        $('title').text(data.currentTarget.title);
        //$('title').text(data.relatedTarget.innerText + ' - 润泰');
      });
      $(document).on('pjax:error', function() {
        console.log("加载失败!");
      });
      $(document).on('pjax:popstate', function(data, status, xhr, options) {});
    }
  };
  alertinfo = function(info, aurl) {
    $('.alert_player').fadeIn();
    $('.alert_player .info').html(info);
    if (aurl === undefined) {} else {
      $('.alert_player .queren_btn,.alert_player .bg').click(function() {
        location.href = aurl;
      })
    }

  };
  backtotop = function(){
    var min_height = 500;
    $(window).scroll(function() {
      //获取窗口的滚动条的垂直位置
      var s = $(window).scrollTop();
      //当窗口的滚动条的垂直位置大于页面的最小高度时，让返回顶部元素渐现，否则渐隐
      if (s > min_height) {
        $(".backtotop").fadeIn(100);
      } else {
        $(".backtotop").fadeOut(200);
      };
    });
    $('.backtotop a').on('click', function(event) {
      event.preventDefault();
      $('body,html').animate({
        scrollTop: 0,
      }, 700);
    });
  };
  initModule = function() {
    scroll();
    router();
    pageajax();
    backtotop();
    animated_contents();
    $('.alert_player .bg,.alert_player .queren_btn').click(function() {
      $('.alert_player').fadeOut();
    });
    $(window).scroll(function() {
      "use strict";
      animated_contents();
    });

  };
  return {
    initModule: initModule
  };
})
